package com.tiamo.chatgpt.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiamo.chatgpt.domain.message.MsgDto;
import com.tiamo.chatgpt.entity.DialogueContentEntity;
import com.tiamo.chatgpt.mapper.DialogueContentMapper;
import com.tiamo.chatgpt.service.DialogueContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 会话内容
 *
 * @author luozhiwei
 * @date 2023/6/5 下午11:40
 */
@Service
@Slf4j
public class DialogueContentServiceImpl extends ServiceImpl<DialogueContentMapper, DialogueContentEntity> implements DialogueContentService {


    @Override
    public boolean insertNewContent(Long userId, Long dialogueId, List<MsgDto> inDbList) {
        if (Objects.isNull(userId) || Objects.isNull(dialogueId) || CollUtil.isEmpty(inDbList)) {
            log.error("插入消息记录。参数异常 userId{},dialogueId{},inDbList{}", userId, dialogueId, JSON.toJSONString(inDbList));
        }

        ThreadUtil.newExecutor(1, 1).execute(() -> {
            List<DialogueContentEntity> entities = inDbList.stream().map(a -> {
                return DialogueContentEntity.builder()
                        .userId(userId)
                        .dialogueId(dialogueId)
                        .role(a.getRole())
                        .content(a.getContent())
                        .build();
            }).collect(Collectors.toList());
            saveBatch(entities);
        });
        return true;
    }
}
